/**
 * 
 */
package cn.gov.msa.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.gov.msa.dao.NavigationMarkMapper;
import cn.gov.msa.dao.WaterwayMapper;
import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.Dictionary;
import cn.gov.msa.model.DictionaryDetail;
import cn.gov.msa.model.NavigationMark;
import cn.gov.msa.model.Org;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.model.Waterway;
import cn.gov.msa.pagehelper.CustomPageHelper;
import cn.gov.msa.service.DictionaryDetailService;
import cn.gov.msa.service.DictionaryService;
import cn.gov.msa.service.WaterwayService;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * @author Administrator
 *
 */
//@Controller
@RestController
@RequestMapping(value = "/waterway", produces="text/plain;charset=UTF-8")
public class WaterwayController {
	@Autowired
	private WaterwayMapper waterwayMapper;
	@Autowired
	private NavigationMarkMapper navigationMarkMapper;

	
	@RequestMapping(value = "/list")
	public String list(@RequestBody JSONObject params, HttpSession session) throws Exception {
		String result = "";
		UserDetail userDetail = (UserDetail) session.getAttribute("userDetail");
		
		int pageNum = 1;
		int pageSize = 10;
		if(params.has("pageNum"))
		{
			pageNum = params.getInt("pageNum");
			params.remove("pageNum");
		}
		if(params.has("pageSize"))
		{
			pageSize = params.getInt("pageSize");
			params.remove("pageSize");
		}
		

		Waterway waterway = (Waterway)JSONObject.toBean(params, Waterway.class);
		if("".equals(params.optString("orgcode"))){
			waterway.setOrgcode(userDetail.getOrgcode());
		}
		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码  
		List<Waterway> waterwayList = waterwayMapper.selectData(waterway);		

	    //获取分页信息
        PageInfo page = new PageInfo(waterwayList);
        

		JSONArray ja = JSONArray.fromObject(waterwayList);
		
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("total", page.getTotal());
		jsonResult.put("rows", ja);
		
		
		result = jsonResult.toString();
		return result;
	}
	
	@RequestMapping(value = "/delete")
	public String delete(String waterwaycode) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{
			int delete = waterwayMapper.deleteByPrimaryKey(waterwaycode);			
			
			result = delete > 0 ? 1 : 0;
			info = delete > 0 ? "删除成功！" : "删除失败！";
		}
		catch(Exception e){
			result = 2;
			info = "删除异常!";
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);
		
		
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/batchDelete")
	public String batchDelete(String waterwaycodes) throws Exception{
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{			
			String waterwaycodeResult = "";
			
			for(int i = 0 ; i < waterwaycodes.split(",").length; i++){	
				String waterwaycode = waterwaycodes.split(",")[i];
				int delete = waterwayMapper.deleteByPrimaryKey(waterwaycode);
				
				if(delete > 0)
					waterwaycodeResult += waterwaycode + ",";
			}
			
			waterwaycodeResult = waterwaycodeResult.substring(0, waterwaycodeResult.length() - 1);

			JSONObject param = new JSONObject();
			param.put("waterwaycode", waterwaycodeResult);
			japaram.add(param);
			
			result = waterwaycodeResult.split(",").length > 0 ? 1 : 0;
			info = waterwaycodeResult.split(",").length > 0 ? "批量删除成功！" : "批量删除失败！";
		}
		catch(Exception e){
			result = 2;
			info = "删除异常!";
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);		
		
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/save")
	public String save(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    int save = 0; 

	    JSONObject jsonResult = new JSONObject();
	    try{
			Waterway waterway = (Waterway)JSONObject.toBean(param, Waterway.class);
		    
		    save = waterwayMapper.insert(waterway);

		    if(save > 0){
		    	result = 1;
		    	info = InfoObjUtil.Success_save_OK;
		    }
		    else{
		    	result = 0;
		    	info = InfoObjUtil.Error_save_no;
		    }
	    }
	    catch(Exception e){
			e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/update")
	public String update(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    int save = 0; 
	    JSONObject jsonResult = new JSONObject();
		
	    try{
			Waterway waterway = (Waterway)JSONObject.toBean(param, Waterway.class);
		    
		    save = waterwayMapper.updateByPrimaryKeySelective(waterway);

		    if(save > 0){
		    	result = 1;
		    	info = InfoObjUtil.Success_save_OK;
		    }
		    else{
		    	result = 0;
		    	info = InfoObjUtil.Error_save_no;
		    }
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	
	@Autowired
	WaterwayService waterwayService;

	@RequestMapping(value = "/savemark")
	public String saveMark(@RequestBody JSONObject param) throws Exception{	
		int result = 0;
		String info = "";
	    boolean save = false; 
	    JSONObject jsonResult = new JSONObject();
		
	    try{
	    	JSONArray jaMark = param.getJSONArray("mark");
	    	
	    	String waterwaycode = jaMark.getJSONObject(0).getString("waterwaycode");
	    	
	    	List<NavigationMark> navigationMarkList = new ArrayList<NavigationMark>();
	    	for (Object object : jaMark) {
	    		NavigationMark navigationMark = (NavigationMark)JSONObject.toBean(JSONObject.fromObject(object), NavigationMark.class);
	    		navigationMarkList.add(navigationMark);
			}

	    	save = waterwayService.saveMark(waterwaycode, navigationMarkList);
	    	
		    if(save){
		    	result = 1;
		    	info = InfoObjUtil.Success_save_OK;
		    }
		    else{
		    	result = 0;
		    	info = InfoObjUtil.Error_save_no;
		    }
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    	result = 2;
	    	info = InfoObjUtil.Exption_msg;
	    }
	    

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
	    
		return jsonResult.toString();
	}
	

	@RequestMapping(value = "/getmark")
	public String getMark(String waterwaycode) throws Exception{
		String result = "";
		String info = "";
		JSONObject jsonResult = new JSONObject();
		
		try{
			List<NavigationMark> navigationMarkList = navigationMarkMapper.selectByWaterwaycode(waterwaycode);		
	
			JSONArray japaram = JSONArray.fromObject(navigationMarkList);
			
			result = japaram.size() > 0 ? "1" : "0";
			info = japaram.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
	
			jsonResult.put("datas", japaram);	
		}
		catch(Exception e){
			result = "2";
			info = InfoObjUtil.Exption_msg;
			e.printStackTrace();
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);	
		
		System.err.println(jsonResult);
		
		return jsonResult.toString();
	}
	


	@RequestMapping(value = "/getinfo")
	public String getInfo(String waterwaycode) throws Exception {
		int result = 0;
		String info = "";
		
		JSONObject joparam = new JSONObject();
		
		try{

			Waterway waterway = waterwayMapper.selectByPrimaryKey(waterwaycode);		
//			
			result = waterway != null ? 1 : 0;
			info = waterway != null ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
			
			joparam = JSONObject.fromObject(waterway);
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", joparam);
		
		System.err.println(jsonResult);

		return jsonResult.toString();
	}
	


	@RequestMapping(value = "/getDictDetal")
	public String getDictDetal(String dictcode) throws Exception {
		int result = 0;
		String info = "";
		
		JSONArray japaram = new JSONArray();
		
		try{

//			List<DictionaryDetail> dictionaryDetailList = dictionaryDetailService.selectDictionary(dictcode, "");
////			
//			result = dictionaryDetailList != null && dictionaryDetailList.size() > 0 ? 1 : 0;
//			info = dictionaryDetailList != null && dictionaryDetailList.size() > 0 ? InfoObjUtil.Success_select_OK : InfoObjUtil.Error_NODATA;
//			
//			japaram = JSONArray.fromObject(dictionaryDetailList);
		}
		catch(Exception e){
			result = 2;
			info = InfoObjUtil.Error_NOINTERFACE;
		}
		
		JSONObject jsonResult = new JSONObject();
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		jsonResult.put("datas", japaram);
		
		System.err.println(jsonResult);

		return jsonResult.toString();
	}
	
}
